﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.Core.Specification;
using Domain.Entities;
using Domain.Service.Order;
using Infrastructure.Order;

namespace DDD从零起步.Controllers
{
    public class OrderController : Controller
    {
        OrderService orderService = new OrderService(
            new OrderRepository(),
            new Application.EmailApplication());

        //
        // GET: /Order/

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            int userid = 0;
            int[] productid;
            int.TryParse(form["UserID"], out userid);
            productid = Array.ConvertAll<string, int>(form.GetValues("Order_Detail.ProductID")
                , (s) => int.Parse(s));
            List<Order_Detail> orderDetail = new List<Order_Detail>();
            Array.ForEach(productid, i =>
            {
                orderDetail.Add(new Order_Detail
                {
                    ProductID = i,
                    ProductName = "测试" + i,
                });
            });
            try
            {
                orderService.GeneratorOrder(new Domain.Entities.Order_Ext
                {
                    OrderInfo = new Domain.Entities.Order_Info
                    {
                        CreateDate = DateTime.Now,
                        UserID = userid,
                        Order_Detail = orderDetail
                    },
                    User_Info = new Domain.Entities.User_Info
                    {
                        UserID = userid,
                        UserName = "zzl",
                        RealName = "占占",
                        Email = "bfyxzls@sina.com"
                    }
                });
                ViewBag.Msg = "下单成功";
            }
            catch (Exception ex)
            {

                ViewBag.Msg = "下单失败，原因" + ex.Message;
            }

            return View();
        }

        public ActionResult List()
        {
            ISpecification<Order_Info> spec = new OrderFromUserSpecification(0);
            var model = orderService.GetOrder_InfoBySpec(spec);
            return View(model);
        }

        [HttpPost]
        public ActionResult List(FormCollection form)
        {
            int userID = 0;
            int.TryParse(form["userid"], out userID);
            ISpecification<Order_Info> spec = new OrderFromUserSpecification(userID);
            var model = orderService.GetOrder_InfoBySpec(spec);
            return View(model);
            return View();
        }
    }
}
